package com.huluobo.a2111douyindemo.setting;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.huluobo.a2111douyindemo.R;
import com.huluobo.a2111douyindemo.databinding.ActivitySettingBinding;
import com.huluobo.a2111douyindemo.utils.ImageLoader;
import com.huluobo.mvvm_base.mvvm.base.view.BaseMVVMActivity;
import com.huluobo.mvvm_base.mvvm.bean.SettingBeanResult;
import com.huluobo.mvvm_base.mvvm.bean.UserBeanResult;
import com.huluobo.mvvm_base.mvvm.db.AppDBUtils;
import com.huluobo.mvvm_base.mvvm.net.Const;
import com.umeng.socialize.net.dplus.db.DBManager;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;

public class SettingActivity extends BaseMVVMActivity<ActivitySettingBinding, SettingViewModel> {

    private UserBeanResult.UserBean userBean;

    @Override
    protected void initData() {
        viewModel.updateIconSuccess.observe(this, settingBeanResult -> {
            //拿到当前的用户信息,重新设置头像
            userBean.setIcon(settingBeanResult.getData());
            //把更新好的用户信息保存到数据中
            AppDBUtils.getInstance().getUserDao().insertUserInfo(userBean);
            ImageLoader.getInstance().loadCircleImage(this, userBean.getIcon(), dataBinding.settingUserIcon);
        });

        viewModel.updateIconFailed.observe(this, s -> {
            Log.i("TagA", "error:" + s);
        });

        viewModel.getUserInfoSuccess.observe(this, userBean -> {
            this.userBean = userBean;
            Log.i("TagA","userBean:"+userBean);
            ImageLoader.getInstance().loadCircleImage(this, userBean.getIcon(), dataBinding.settingUserIcon);
            if (userBean.getAddress() != null) {
                dataBinding.settingAddressEt.setText(userBean.getAddress());
            }
            if (userBean.getNickname() != null) {
                dataBinding.settingNickNameEt.setText(userBean.getNickname());
            }
            if (userBean.getBirth() != null) {
                dataBinding.settingBrithEt.setText(userBean.getBirth());
            }
            if (userBean.getSex() != null) {
                dataBinding.settingSexEt.setText(userBean.getSex());
            }
        });

        viewModel.getUserInfoFailed.observe(this, s -> {
            Log.i("TagA", "获取用户信息失败:" + s);
        });

        viewModel.updateAddressSuccess.observe(this, settingBeanResult -> {
            if (settingBeanResult.getCode() == 200) {
                dataBinding.settingAddressEt.setText(settingBeanResult.getData());
            }
        });

        viewModel.updateAddressFailed.observe(this, s -> {
            Log.i("TagA", "更新用户地址失败::" + s);
        });

        viewModel.updateNicknameSuccess.observe(this, settingBeanResult -> {
            if (settingBeanResult.getCode() == 200) {
                dataBinding.settingNickNameEt.setText(settingBeanResult.getData());
            }
        });

        viewModel.updateNicknameFailed.observe(this, s -> {
            Log.i("TagA", "更新用户昵称失败::" + s);
        });

        viewModel.updateBrithSuccess.observe(this, settingBeanResult -> {
            if (settingBeanResult.getCode() == 200) {
                dataBinding.settingBrithEt.setText(settingBeanResult.getData());
            }
        });

        viewModel.updateBrithFailed.observe(this, s -> {
            Log.i("TagA", "更新用户生日失败::" + s);
        });

        viewModel.updateSexSuccess.observe(this, settingBeanResult -> {
            if (settingBeanResult.getCode() == 200) {
                dataBinding.settingSexEt.setText(settingBeanResult.getData());
            }
        });

        viewModel.updateSexFailed.observe(this, s -> {
            Log.i("TagA", "更新用户性别失败::" + s);
        });
    }

    @Override
    protected void initView() {
        //进入设置页面查询当前的用户信息,为了之后做修改用
        viewModel.getUserInfo(SPUtils.getInstance().getString(Const.SP_USERNAME));
        dataBinding.settingUserIcon.setOnClickListener(v -> {
            selectLocPic();
        });


        dataBinding.settingAddressBtn.setOnClickListener(v -> {
            viewModel.updateAddress(dataBinding.settingAddressEt.getText().toString());
        });

        dataBinding.settingNickNameBtn.setOnClickListener(v -> {
            viewModel.updateUserNickname(dataBinding.settingNickNameEt.getText().toString());
        });

        dataBinding.settingBrithBtn.setOnClickListener(v -> {
            viewModel.updateBrith(dataBinding.settingBrithEt.getText().toString());
        });

        dataBinding.settingSexBtn.setOnClickListener(v -> {
            viewModel.updateSex(dataBinding.settingSexEt.getText().toString());
        });
    }

    private void selectLocPic() {
        PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture();
    }

    //图片选择器的回调
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            PictureBean bean = bundle.getParcelable(PictureSelector.PICTURE_RESULT);
            String filePath = bean.getPath();
            if (!TextUtils.isEmpty(filePath)) {
                viewModel.updateUserIcon(filePath);
            }
        }
    }

    @Override
    protected Class<SettingViewModel> getViewModel() {
        return SettingViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }
}